import list from './valueList'

export default {
  install (Vue) {
    Vue.filter('whether', value => {
      if (value == 0) return '否'
      if (value == 1) return '是'
    })
    Vue.filter('emnu', (value, key) => {
      if (!list[key]) return value
      const item = list[key].find(v => v.value === value)
      return item ? item.name : value
    })
    Vue.filter('filesize', value => {
      let val = parseInt(value)
      if (!val) return '0 B'
      if (val < 1024) return val + ' B'
      if (val >= 1024 && val < 1048576) return (val / 1024).toFixed(1) + ' KB'
      if (val >= 1048576 && val < 1073741824) return (val / 1048576).toFixed(1) + ' MB'
      return (val / 1073741824).toFixed(1) + ' GB'
    })
    Vue.filter('num', value => {
      if (!value || value === '0') return '0'
      value = parseInt(value)
      let _value = value
      value = (value + '').replace('-', '')
      let newStr = ''
      let count = 0
      for (let i = value.length - 1; i >= 0; i--) {
        if (count % 3 === 0 && count !== 0) {
          newStr = value.charAt(i) + ',' + newStr
        } else {
          newStr = value.charAt(i) + newStr
        }
        count++
      }
      if (_value < 0) newStr = '-' + newStr
      return newStr
    })
  }
}
